package com.framework.dubbo.producer.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.ReUtil;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 描述：
 * html 尺寸转换工具
 * 1pt=1px*4/3
 * 16px = 1em
 * @author xianggj
 * @Date 2021/10/28 17:12
 **/
public class ConvertUnitUtils {
    public static void main(String[] args) throws Exception {
        TimeInterval timer = DateUtil.timer();
        File file = new File("D:\\sinux\\pdf\\index3.html");
        //正则匹配
        Pattern patternFont = getPatternFont();
        //正则匹配
        Pattern patternAll = getPatternAll();
        //1.创建Reader对象
        SAXReader reader = new SAXReader();
        //2.加载xml
        Document document = reader.read(file);
        //3.获取根节点
        Element rootElement = document.getRootElement();
        rootElement.addAttribute("style","font-size:62.5%");
        recursion(patternAll, patternFont, rootElement, 1);
        //实例化输出格式对象
        OutputFormat format = OutputFormat.createPrettyPrint();
        //设置输出编码
        format.setEncoding("UTF-8");
        File file1 = new File("D:\\sinux\\pdf\\html\\ccf1.html");
        XMLWriter writer = new XMLWriter(new FileOutputStream(file1), format);
        writer.write(document);
        System.out.println(timer.interval() + "ms");
    }

    static void recursion(Pattern patternAll, Pattern patternFont, Element rootElement, int level) {
        Iterator iterator = rootElement.elementIterator();
        while (iterator.hasNext()){
            Element stu = (Element) iterator.next();
            List<Attribute> attributes = stu.attributes();
            for (Attribute attribute : attributes) {
                String value = attribute.getValue();
                value = pxToEm(patternAll, value, level);
                value = pxToEmFont(patternFont, value);
                attribute.setValue(value);
            }
            recursion(patternAll, patternFont, stu, level + 1);
        }
    }

    private static Pattern getPatternFont() {
        String pattern = "(font-size:|top:|left:|line-height:|width:|height:)((-?[1-9]\\d*\\.?\\d*)|(-?0\\.\\d*[1-9]))(rem)";
        Pattern compile = Pattern.compile(pattern);
        return compile;
    }

    private static Pattern getPatternAll() {
        String pattern = "((-?[1-9]\\d*\\.?\\d*)|(-?0\\.\\d*[1-9]))(px|pt)";
        Pattern compile = Pattern.compile(pattern);
        return compile;
    }

    private static String pxToEm(Pattern compile, String value, int level) {
        List<String> allGroups = ReUtil.findAll(compile, value,0);
        for (String ptStr : allGroups) {
            String substring = StringUtils.substring(ptStr, 0, -2);
            Double aDouble = Double.valueOf(substring);
            if (ptStr.endsWith("pt")){
                aDouble = aDouble *4/3;
            }
            Double newDouble = aDouble/(16*Math.pow(0.625,level-1));
            String newStr = String.format("%.6f", newDouble) + "rem";
            value = value.replace(ptStr, newStr);
        }
        return value;
    }
    private static String pxToEmFont(Pattern compile, String value) {
        List<String> allGroups = ReUtil.findAll(compile, value,0);
        for (String ptStr : allGroups) {
            int i = ptStr.indexOf(":");
            String header = StringUtils.substring(ptStr, 0,i +1);
            String substring = StringUtils.substring(ptStr, i +1, -3);
            Double aDouble = Double.valueOf(substring);
            Double newDouble = aDouble*0.625;
            String newStr = header+ String.format("%.6f", newDouble) + "rem";
            value = value.replace(ptStr, newStr);
        }
        return value;
    }
}
